package hbt;

import hbt.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import modelo.Proveedor;

public class HibernateProveedorDAO {
	private static HibernateProveedorDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateProveedorDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateProveedorDAO();
		} 
		return instancia;
	}
	
	public void grabarProveedor(Proveedor prov){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(prov);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarProveedor(Proveedor p) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(p);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarProveedor(Proveedor p) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(p);
		session.flush();
		session.getTransaction().commit();
		session.close();
		
	}
	
	public Proveedor consultarProveedor(int idprov) {
		Session session = sf.openSession();
		session.beginTransaction();
		Proveedor p = (Proveedor) session.get(Proveedor.class, idprov);
		return p;
		
	}
	
	
}
